
import 'package:flutter/cupertino.dart';
import 'package:hxdj/common/my_header_global.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

/*
用法示例：
MySmartRefresher(
  scrollController: scrollController,
  controller: controller,
  noMoreData: ifNoMoreData, // 没有更多了记得传个true
  onLoading: () {
    doRequestData();
  },
  enablePullDown: ifEnablePullDown, // 默认true
  enablePullUp: ifEnablePullUp, // 默认true
  onRefresh: () {
    doRequestData();
  },
  itemBuilder: (context, index){
    return yourItemBuilder(index);
  },
  dataList: allListData, // 列表数据集合
  emptyWidget: yourEmptyWidget(), // 空样式
),
*/
class MySmartRefresher extends StatefulWidget {
  final ScrollPhysics physics; // 滚动方式
  final Function? onLoading; // 加载中触发的回调
  final Function? onRefresh; // 下拉刷新回调
  final bool enablePullDown; //是否允许下拉刷新
  final bool enablePullUp; // 是否允许上拉加载
  final RefreshController controller; // 刷新控制器
  final ScrollController scrollController; // 滚动控制器
  final List<dynamic> dataList; // 渲染出来的数据集合
  final bool isDegraded; // 是否开启了降级
  final bool noMoreData; // 是否展示 没有更多了
  final Widget? emptyWidget; // 空数据样式
  final IndexedWidgetBuilder? itemBuilder; // ItemBuilder
  final bool shrinkWrap;
  final Widget? refreshingIcon; //下拉刷新Icon样式
  final Widget? loadingIcon; // 上拉加载Icon样式
  final Widget? headerWidget; // 列表顶部的头部组件(如果你需要)
  final Widget? customChildWidget; // 自定义子组件
  final Widget? customNoMoreWidget; // 自定义底部没有更多结果组件


  final String headerReleaseText;
  final String headerRefreshingText;
  final String headerCompleteText;
  final String headerIdleText;
  final String headerFailedText;

  final String footerLoadingText;
  final String footerNoDataText;
  final String footerIdleText;
  final String footerFailedText;
  final String footerCanLoadingText;


  const MySmartRefresher({ 
    super.key,
    required this.controller,
    required this.scrollController,
    this.itemBuilder,
    this.onLoading,
    this.onRefresh,
    this.physics = const AlwaysScrollableScrollPhysics(),
    this.dataList = const [],
    this.enablePullDown = true,
    this.enablePullUp = false,
    this.isDegraded = false,
    this.shrinkWrap = true,
    this.noMoreData = false,
    this.emptyWidget,
    this.refreshingIcon,
    this.loadingIcon,
    this.headerWidget,
    this.customChildWidget,
    this.customNoMoreWidget,

    this.headerReleaseText = "释放以刷新数据",
    this.headerRefreshingText = "刷新中...",
    this.headerCompleteText = "刷新完毕",
    this.headerIdleText = "下拉刷新数据",
    this.headerFailedText = "刷新失败",

    this.footerLoadingText = "加载中，请稍后",
    this.footerNoDataText = "没有更多了~",
    this.footerIdleText = "上拉以加载更多数据",
    this.footerFailedText = "加载失败",
    this.footerCanLoadingText = "上拉以加载更多数据",

  });

  @override
  State<MySmartRefresher> createState() => _MySmartRefresherState();
}

class _MySmartRefresherState extends State<MySmartRefresher> {
  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      physics: widget.physics,
      controller: widget.controller,
      scrollController: widget.scrollController,
      primary: false,  // 默认不使用它提供的controller
      header: ClassicHeader(
        releaseText: widget.headerReleaseText,
        refreshingText: widget.headerRefreshingText,
        completeText: widget.headerCompleteText,
        idleText: widget.headerIdleText,
        failedText: widget.headerFailedText,
        refreshingIcon: widget.refreshingIcon ?? refreshingIconWidget(),
      ),
      footer: ClassicFooter(
        loadingText: widget.footerLoadingText,
        noDataText: widget.footerNoDataText,
        idleText: widget.footerIdleText,
        failedText: widget.footerFailedText,
        canLoadingText: widget.footerCanLoadingText,
        loadingIcon: widget.loadingIcon ?? loadingIconWidget(),
        outerBuilder:(child) => widget.customNoMoreWidget ?? outerFooterWidget(child)
      ),
      onLoading:() async {
        widget.onLoading?.call();
      },
      onRefresh: () async {
        widget.onRefresh?.call();
      },
      enablePullDown: widget.enablePullDown,
      enablePullUp: widget.enablePullUp,
      child: deliverWidget(),
    );
  }


  // 分发Widget
  Widget deliverWidget(){
    return 
    widget.customChildWidget ?? ListView.builder( // 正常情况
      controller: widget.scrollController,
      shrinkWrap: widget.shrinkWrap,
      itemCount: widget.dataList.length, // 此处第一个1是头布局 然后是中间item 数据 最后一个1 是没有更多了组件
      itemBuilder: (context, index){
                return widget.itemBuilder!(context, index);
      },
    );
  }

  Widget outerFooterWidget(Widget child){
    return Container(
      margin: const EdgeInsets.only(top: 20),
      child: child,
    );
  }

  // 下拉刷新icon
  Widget refreshingIconWidget(){
    return const CupertinoActivityIndicator(
      radius: 10,
    );
  }

  // 上拉加载icon
  Widget loadingIconWidget(){
    return const CupertinoActivityIndicator(
      radius: 10,
    );
  }

  // 没有更多了组件 解决没有更多了组件经常出问题的问题
  Widget noMoreDataWidget(){
    return Container(
      color: MyColours.appMain,
      padding: const EdgeInsets.only(top: 24, bottom: 48),
      alignment: Alignment.center,
      child: Text(
        widget.footerNoDataText,
        overflow: TextOverflow.ellipsis,
        style: const TextStyle(
          color: MyColours.color888888,
          fontSize: 16,
        ),
      ),
    );
  }

}